package net.neiquan.applibrary.wight;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import net.neiquan.applibrary.R;

import java.util.ArrayList;


/**
 * 作者 ： 邓勇军
 * 时间 ： 2016/2/27.
 * version:1.0
 */

public class RefreshLayout extends LinearLayout {

    @SuppressWarnings("unused")
    private ArrayList<View> mDefaultViews;
    private View loadingView;
    private View failView;
    private View emptyView;
    private Context context;
    private RetryListener retryListener;

    public RetryListener getRetryListener() {
        return retryListener;
    }

    public void setRetryListener(RetryListener retryListener) {
        this.retryListener = retryListener;
        // for Lazy loading
        if (failView != null) {
            failView.findViewById(R.id.exception_button).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    RefreshLayout.this.retryListener.onRetryClick();
                }
            });
        }
    }

    public RefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    public void initDeaultView() {
        loadingView = LayoutInflater.from(context).inflate(R.layout.load_ing, null, false);
        failView = LayoutInflater.from(context).inflate(R.layout.load_fail, null, false);
        emptyView = LayoutInflater.from(context).inflate(R.layout.load_empty, null, false);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        lp.gravity = Gravity.CENTER;
        addView(loadingView, 0, lp);
        addView(failView, 1, lp);
        addView(emptyView, 2, lp);
        loadingView.setVisibility(View.GONE);
        failView.setVisibility(View.GONE);
        emptyView.setVisibility(View.GONE);
        if (retryListener != null) {
            failView.findViewById(R.id.exception_button).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    retryListener.onRetryClick();
                }
            });
        }
    }

    public void setMarginTop(int margintop) {
        if (emptyView == null) {
            initDeaultView();
        }
        LayoutParams lp = (LayoutParams) failView.getLayoutParams();
        lp.setMargins(0, margintop, 0, 0);
        loadingView.setLayoutParams(lp);
        LayoutParams lp2 = (LayoutParams) loadingView.getLayoutParams();
        lp2.setMargins(0, margintop, 0, 0);
        loadingView.setLayoutParams(lp2);
        LayoutParams lp3 = (LayoutParams) emptyView.getLayoutParams();
        lp3.setMargins(0, margintop, 0, 0);
        emptyView.setLayoutParams(lp2);
    }

    public void showLoadView() {
        if (emptyView == null) {
            initDeaultView();
        }
        failView.setVisibility(View.GONE);
        emptyView.setVisibility(View.GONE);
        loadingView.setVisibility(View.VISIBLE);
    }

    public void showEmptyView() {
        if (emptyView == null) {
            initDeaultView();
        }
        loadingView.setVisibility(View.GONE);
        failView.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
    }

    public void showEmptyView(String message) {
        if (emptyView == null) {
            initDeaultView();
        }
        loadingView.setVisibility(View.GONE);
        failView.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
        ((TextView) emptyView.findViewById(R.id.laodMessage)).setText(message);
    }

    public void showEmptyView(int messageId) {
        if (emptyView == null) {
            initDeaultView();
        }
        loadingView.setVisibility(View.GONE);
        failView.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
        ((TextView) emptyView.findViewById(R.id.laodMessage)).setText(messageId);
    }

    public void showFailView() {
        if (emptyView == null) {
            initDeaultView();
        }
        emptyView.setVisibility(View.GONE);
        loadingView.setVisibility(View.GONE);
        failView.setVisibility(View.VISIBLE);

    }

    public void hideAll() {
        if (emptyView != null) {
            loadingView.setVisibility(View.GONE);
            failView.setVisibility(View.GONE);
            emptyView.setVisibility(View.GONE);
        }
    }

    /**
     * @author wang
     * @version V1.0
     * @Description: this is interface for retry on error
     * @date 2015-7-24 下午6:23:06
     */
    public interface RetryListener {
        public void onRetryClick();
    }
}
